﻿var token = localStorage.getItem("token");
$(document).ready(function () {
    ShowTime();
});
// 表单提交事件
var Datetimenow = new Date();
var year = Datetimenow.getFullYear() + '-' + (Datetimenow.getMonth() + 1) + '-' + Datetimenow.getDate() + ' ' + Datetimenow.getHours() + ':' + Datetimenow.getMinutes() + ':' + Datetimenow.getSeconds();
function ShowTime() {
    var ids = [];
    var cheddata = [];
    
    layui.use('table', function () {
        var table = layui.table;
        var layer = layui.layer;

        //渲染表格的函数
        function renderTable(pageNum) {
            //获取查询条件
            var UnitCode = $("#UnitCode").val();
            var UnitName = $("#UnitName").val();
            let unit = {};
            unit.UnitCode = UnitCode;
            unit.UnitName = UnitName;
            unit.PageIndex = pageNum;
            unit.PageSize = 100;
            unit.Token = token;
            //发送AJAX请求
            $.ajax({
                url: '/Unit/GetUnitShow',
                type: 'post',
                data: { unit: unit },
                dataType: 'json',
                success: function (res) {
                    var data = JSON.parse(res)
                    console.log(data)
                    //渲染表格
                    table.render({
                        elem: '#dataTable',
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'unitCode', title: '单位编号' },
                            { field: 'unitName', title: '单位名称' },
                            { field: 'uremark', title: '备注' },
                            //操作列
                            {
                                title: '操作',
                                templet: function (d) {
                                    //编辑
                                    return ' <button type="button" onclick="editItem(' + d.unitId + ')" class="layui-btn layui-btn-xs">编辑</button>' + ' <button type="button" onclick="deleteItem(' + d.unitId + ')" class="layui-btn layui-btn-xs">删除</button>'
                                }
                            },
                        ]],
                        toolbar: "#toolbarDemo",
                        defaultToolbar: ["exports"],
                        data: data.data.list,  // 数据源
                        page: true,   // 启用分
                        limit: 10,    // 每页显示多少条数据
                        limits: [10, 20, 30],  // 分页选择项
                        curr: pageNum,  // 当前页
                        total: data.data.listcount,  // 总记录数
                        loading: true // 启用加载状态
                    });
                },
                Error: function (xhr, status, error) {
                    console.log("请求失败" + error);
                }
            });
        }
        // 初次加载表格数据
        renderTable(1);
        // 查询按钮点击事件
        $('#searchBtn').click(function () {
            renderTable(1);  // 重置为第一页，并重新加载数据
        });
        //刷新
        $("#Unitrefresh").click(function () {
            location.reload();
        })
        // 触发表格复选框选择
        table.on('checkbox(dataTableFilter)', function (obj) {
            cheddata = table.checkStatus('dataTable').data;
            ids = cheddata.map(function (item) {
                return item.unitId;
            })
            console.log(ids);
        });
        //////////批量删除////////
        $('#BathDel').click(function () {
            if (ids.length == 0) {
                layer.msg('请先选择要删除的项', { icon: 2 });
                return;
            }
            // 询问是否删除
            layer.confirm('确定删除吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                let units = {};
                units.unitId = ids;
                units.delName = "呓语";
                units.delTime = year;
                units.Token = token;
                //执行删除操作
                $.ajax({
                    url: '/Unit/DelAllUnits',
                    type: 'post',
                    data: { unit: units },
                    success: function (res) {
                        var s = JSON.parse(res)
                        if (s.data > 0) {
                            layer.msg('删除成功', { icon: 1 }, function () {
                                location.reload()
                            });
                        }
                        else {
                            layer.msg('删除失败');
                            return
                        }
                    }
                })
            }, function () {
                layer.msg('已取消', {
                    time: 20000, //20s后自动关闭
                    btn: ['确定']
                });
            });
        });
    })
    ///////////////////////////////////编辑/////////////////
    window.editItem = function (UnitId) {
        UnitFt(UnitId);
    };
    /////删除//////
    window.deleteItem = function (UnitId) {
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.confirm('你确定要删除这条数据吗？', {
                title: '提示'
            }, function (index) {
                let unit = {};
                unit.unitId = UnitId;
                unit.delName = "呓语";
                unit.delTime = year;
                unit.Token = token;
                //执行删除操作
                $.ajax({
                    url: '/Unit/DelUnits',
                    type: 'post',
                    data: { unit: unit },
                    success: function (res) {
                        var s = JSON.parse(res)
                        if (s.data > 0) {
                            layer.msg('删除成功', { icon: 1 }, function () {
                                location.reload()
                            });
                        }
                        else {
                            layer.msg('删除失败');
                            return
                        }
                    }
                })
            });
        });
    };
}

//添加弹出层
layui.use(function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    // 事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                area: '800px',
                resize: false,
                shadeClose: true,
                title: '新增单位',
                content: `
                                <div class="layui-form" lay-filter="filter-test-layer" style="margin: 16px;">
                                   <table>
                                        <tr>
                                           <td>
                                              <div class="layui-form-item" style="float:left">
                                                 <div class="layui-input-group">
                                                    <div class="layui-input-split layui-input-prefix">
                                                        单位编号：
                                                     </div>
                                                           <input type="text" lay-verify="inputValue" id="UnitCodes" name="UnitCode" placeholder="请输入" class="layui-input">
                                                      </div>
                                                  </div>
                                                </td>
                                                <td>
                                                  <div class="layui-form-item" style="float:left">
                                                      <div class="layui-input-group">
                                                          <div class="layui-input-split layui-input-prefix">
                                                               单位名称：
                                                           </div>
                                                                     <input type="text" lay-verify="required" id="UnitName" name="UnitName" placeholder="请输入" class="layui-input">
                                                       </div>
                                                     </div>
                                                  </td>
                                                     </tr>
                                                          <tr>
                                                              <td>
                                                                  <div class="layui-form-item layui-form-text">
                                                                      <label class="layui-form-label">描述:</label>
                                                                      <div class="layui-input-block">
                                                                                      <textarea placeholder="请输入内容" lay-verify="required" id="Uremark"  name="Uremark" class="layui-textarea"></textarea>
                                                                      </div>
                                                                  </div>
                                                              </td>
                                                          </tr>
                                                      </table>
                                                <div class="layui-form-item">
                                                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login">提交</button>
                                                </div>
                                                    </div>
                                `,
                success: function () {
                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    //点击提交添加
                    var code = Math.floor(Math.random() * 1000000) + 100000;
                    $("#UnitCodes").val(code)
                    //表单提交
                    form.on('submit(demo-login)', function (data) {
                        var field = data.field;//获取表单字段值
                        
                        let obj1 = {
                            UnitCode: field.UnitCode,
                            UnitName: field.UnitName,
                            Uremark: field.Uremark,
                            Office_Id: '18591113953464852',
                            User_Id: '1',
                            AddName: '呓语',
                            AddTime: year,
                            UpdName: null,
                            UpdTime: null,
                            Token: token
                        };
                        $.ajax({
                            url: '/Unit/AddUnits',
                            data: { obj: obj1 },
                            type: 'post',

                            success: function (res) {
                                var s = JSON.parse(res)
                                if (s.data > 0) {
                                    layer.msg('添加成功', { icon: 1 }, function () {
                                        location.reload()
                                    });
                                }
                                else {
                                    layer.msg('添加失败');
                                    return;
                                }
                            }
                        })
                        return false;
                    })
                }
            });
        }
    })
});
//反填
function UnitFt(UnitId) {
    let obj = {};
    let funit = {
        UnitId : UnitId,
        Token : token
    };
    $.ajax({
        url: '/Unit/GetUnitFt',
        type: 'get',
        data:  funit,
        success: function (res) {
            var data = JSON.parse(res);
            console.log(data)
            $(data.data).each(function () {
                $("#UnitId").val(this.unitId);
                $("#UpdUnitCode").val(this.unitCode);
                $("#UpdUnitName").val(this.unitName);
                $("#UpdUremark").val(this.uremark);
                $("#Office_Id").val(this.Office_Id);
                $("#User_Id").val(this.User_Id);
                $("#AddName").val(this.AddName);
                $("#AddTime").val(this.AddTime);
                $("#UpdName").val(this.UpdName);
                $("#UpdTime").val(this.UpdTime);


                obj.UnitId = this.unitId;
                obj.UnitCode = this.updUnitCode;
                obj.UnitName = this.updUnitName;
                obj.Uremark = this.updUremark;
                obj.Office_Id = this.office_Id;
                obj.User_Id = this.user_Id;
                obj.AddName = this.addName;
                obj.AddTime = this.addTime;
                obj.UpdName = '呓语';
                obj.UpdTime = '2024-11-25';

            })
        }
    })
    layui.use(function () {
        var $ = layui.$;
        var layer = layui.layer;
        var util = layui.util;
        var form = layui.form;
        // 事件
        layer.open({
            type: 1, // page 层类型
            area: '800px',
            title: '修改单位',
            content: ` <table>
                                        <tr>
                                             <td>
                                                <div class="layui-form-item" style="float:left">
                                                   <div class="layui-input-group">
                                                      <div class="layui-input-split layui-input-prefix">
                                                          单位编号：
                                                       </div>
                                                                   <input type="text" id="UpdUnitCode" name="UpdUnitCode" placeholder="请输入" class="layui-input">
                                                        </div>
                                                    </div>
                                                  </td>
                                                     <td>
                                                        <div class="layui-form-item" style="float:left">
                                                            <div class="layui-input-group">
                                                                <div class="layui-input-split layui-input-prefix">
                                                                     单位名称：
                                                                 </div>
                                                                            <input type="text" id="UpdUnitName" name="UpdUnitName" placeholder="请输入" class="layui-input">
                                                                 </div>
                                                               </div>
                                                             </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                <div class="layui-form-item layui-form-text">
                                                                    <label class="layui-form-label">描述:</label>
                                                                    <div class="layui-input-block">
                                                                                    <textarea placeholder="请输入内容" id="UpdUremark" name="UpdUremark" class="layui-textarea"></textarea>
                                                                    </div>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                      </table >
                                    <div class="layui-form-item">
                                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login">提交</button>
                              </div>
                    `,
            success: function () {
                var obj1 = {
                    UnitId: '',
                    UnitCode: '',
                    UnitName: '',
                    Uremark: '',
                    Office_Id: '',
                    User_Id: '',
                    AddName: '',
                    AddTime: '',
                    UpdName: '',
                    UpdTime: ''
                };
                form.render();
                //表单提交
                form.on('submit(demo-login)', function (data) {
                    var field = data.field;//获取表单字段值

                    obj1.UnitId = obj.UnitId;
                    obj1.UnitCode = $("#UpdUnitCode").val();
                    obj1.UnitName = $("#UpdUnitName").val();
                    obj1.Uremark = $("#UpdUremark").val();
                    obj1.Office_Id = obj.Office_Id;
                    obj1.User_Id = obj.User_Id;
                    obj1.AddName = obj.AddName;
                    obj1.AddTime = obj.AddTime;
                    obj1.UpdName = '呓语';
                    obj1.UpdTime = year;
                    obj1.Token = token;
                    $.ajax({
                        url: '/Unit/UpdUnits',
                        data: { obj: obj1 },
                        type: 'put',

                        success: function (res) {
                            var s = JSON.parse(res)
                            //layui - icon - success
                            if (s.data > 0) {
                                layer.msg('修改成功', { icon: 1 }, function () {
                                    location.reload()
                                });
                            }
                            else {
                                layer.msg('修改成功');
                            }
                        }
                    })
                    return false;
                })
            }
        });
    });
}
//取消
function QX() {
    location.reload();
}
